package com.ymatou.iapi.idcardquery.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.common.lang.CipherUtil;
import com.ymatou.iapi.idcardquery.parameter.GetIdCardInfoByBuyerIdAndNameAndPhoneReq;
import com.ymatou.iapi.idcardquery.resp.GetSingleIdCardInfoResp;
import com.ymatou.iapi.idcardquery.resp.IdCardInfo;
import com.ymatou.iapi.idcardquery.resp.IdCardInfoVerify;
import com.ymatou.iapi.idcardquery.resp.RejectReasonEnum;
import com.ymatou.iapi.idcardquery.service.GetIdCardInfoByBuyerIdAndNameAndPhoneCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.database.model.YmtIdCardInfo;
import com.ymttest.database.sqlwapper.YmtIdCardInfoWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*根据买家Id + 收件人 + 手机号 获取身份证信息(本地 + 贝海)
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetIdCardInfoByBuyerIdAndNameAndPhone {
	private static GetIdCardInfoByBuyerIdAndNameAndPhoneReq bean;
	private static GetIdCardInfoByBuyerIdAndNameAndPhoneCall call;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据买家Id + 收件人 + 手机号 获取身份证信息(本地 + 贝海)接口");
	}

	@Before
	public void caseUp() {
		bean = new GetIdCardInfoByBuyerIdAndNameAndPhoneReq();
		call = new GetIdCardInfoByBuyerIdAndNameAndPhoneCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	private void verifyDb() throws Exception {

		YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;
		String oldKey = "1234567890123456";
		String newKey = "123456789";
		GetSingleIdCardInfoResp getSingleIdCardInfoResp = call
				.getSingleIdCardInfoResp();

		Logger.comment("校验身份证信息表ymt_idcardinfo");

		YmtIdCardInfo expectedIdCardInfo = ymtIdCard.selectByNameAndBuyerId(
				bean.getName(), bean.getBuyerId());

		IdCardInfo actualIdCardInfo = getSingleIdCardInfoResp.getIdCardInfo();

		Logger.comment("校验name:" + expectedIdCardInfo.getName());

		Logger.verifyEquals(expectedIdCardInfo.getCardId(),
				actualIdCardInfo.getCardId(), "校验CardId");

		Logger.verifyEquals(expectedIdCardInfo.getName(),
				actualIdCardInfo.getName(), "校验name");
		Logger.verifyEquals(
				CipherUtil.decryptAES(oldKey, expectedIdCardInfo.getIDCard()),
				actualIdCardInfo.getCardNumber(), "校验CardNumber");

		Logger.verifyEquals(expectedIdCardInfo.getCardType(),
				actualIdCardInfo.getCardType(), "校验CardType");

		Logger.verifyEquals(expectedIdCardInfo.getStatus(),
				actualIdCardInfo.getStatus(), "校验Status");

		Logger.verifyEquals(
				CipherUtil.decryptAES(newKey, expectedIdCardInfo.getMobile()),
				actualIdCardInfo.getMobile(), "校验Mobile");

		String rejectReason = null;

		int rejectReasonCode = expectedIdCardInfo.getRejectReason();

		if (expectedIdCardInfo.getStatus() == -1) {

			RejectReasonEnum rejectReasonEnum = RejectReasonEnum

			.getEnum(rejectReasonCode);

			rejectReason = "审核未通过";

			if (null != rejectReasonEnum) {
				rejectReason = rejectReasonEnum.getMessage();
			}
		}

		String expectedPicRightSide = "";

		String expectedPicReverseSide = "";

		//boolean isHasPic = true;

		if (!expectedIdCardInfo.getRightSide().isEmpty()) {

			String picUrl = IdCardInfoVerify.convertPicUrl(expectedIdCardInfo
					.getRightSide());

			expectedPicRightSide = picUrl;

		} /*else {
			isHasPic = false;

			}*/

		if (!expectedIdCardInfo.getReverseSide().isEmpty()) {

			String picUrl = IdCardInfoVerify.convertPicUrl(expectedIdCardInfo
					.getReverseSide());
			expectedPicReverseSide = picUrl;
		} /*else {
			isHasPic = false;
			}*/

		Logger.verifyEquals(expectedIdCardInfo.getHasPic(),
				actualIdCardInfo.getHasPic(), "校验HasPic");

		Logger.verifyEquals(rejectReason, actualIdCardInfo.getRejectReason(),
				"校验RejectReason");

		Logger.verifyEquals(expectedPicRightSide,
				actualIdCardInfo.getPicRightSide(), "校验picRightSide");
		Logger.verifyEquals(expectedPicReverseSide,
				actualIdCardInfo.getPicReverseSide(), "校验PicReverseSide");

		Logger.verifyEquals(expectedIdCardInfo.getCheckIdCardStatus(),
				actualIdCardInfo.getCheckIdCardStatus(), "校验CheckIdCardStatus");

		DataUtil.verifyEquals(expectedIdCardInfo.getUploadTime(),
				actualIdCardInfo.getUploadTime(), "验证UploadTime");

	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetIdCardInfoByBuyerIdAndNameAndPhone_001() {
		Logger.start(true, "根据买家id、姓名,手机号从本地库查询成功");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));
			String key = "123456789";
			YmtIdCardInfoWapper ymtIdCard = new YmtIdCardInfoWapper();;

			YmtIdCardInfo ymtIdCardInfo = ymtIdCard.selectByBuyerId(buyerId);

			String name = ymtIdCardInfo.getName();
			String phone = CipherUtil.decryptAES(key, ymtIdCardInfo.getMobile());

			bean.setBuyerId(buyerId);
			bean.setName(name);
			bean.setPhone(phone);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetIdCardInfoByBuyerIdAndNameAndPhone_002() {
		Logger.start(true, "本地库没有，根据买家id、姓名,手机号从贝海查询成功,发送mq消息通知身份证管理服务");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			String name = "孙军杰";
			String phone = "13501815733";

			IdCardInfoVerify.callDeleteIdCardInfo(buyerId, name);

			bean.setBuyerId(buyerId);
			bean.setName(name);
			bean.setPhone(phone);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetIdCardInfoByBuyerIdAndNameAndPhone_003() {
		Logger.start(true, "收件人为null");
		try {

			//String name = "素还真";
			String phone = "13800000002";

			bean.setBuyerId(288484l);
			//bean.setName(name);
			bean.setPhone(phone);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");

			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");

			Logger.verifyEquals("name:收件人未提供", call.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetIdCardInfoByBuyerIdAndNameAndPhone_004() {
		Logger.start(true, "buyerId为null");
		try {

			String name = "素还真";
			String phone = "13800000002";

			//bean.setBuyerId(buyerId);
			bean.setName(name);
			bean.setPhone(phone);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");

			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");

			Logger.verifyEquals("buyerId:buyerId未提供", call.getString("msg"),
					"验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetIdCardInfoByBuyerIdAndNameAndPhone_005() {
		Logger.start(true, "手机号为null");
		try {

			String name = "素还真";
			//String phone = "13800000002";

			bean.setBuyerId(288484L);
			bean.setName(name);
			//bean.setPhone(phone);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

			Logger.verifyEquals("0", call.getString("bcode"), "验证bcode");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetIdCardInfoByBuyerIdAndNameAndPhone_006() {
		Logger.start(true, "name为null");
		try {

			//String name = "素还真";
			String phone = "13800000002";

			bean.setBuyerId(288484L);
			//bean.setName(name);
			bean.setPhone(phone);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("400", call.getString("code"), "验证Code");

			Logger.verifyEquals("100", call.getString("bcode"), "验证bcode");

			Logger.verifyEquals("name:收件人未提供", call.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetIdCardInfoByBuyerIdAndNameAndPhone_007() {
		Logger.start(true, "本地库没有,贝海也没记录");
		try {

			long buyerId = Long.valueOf(EnvSetup.getData("SellerId2"));

			String name = TestDataManager.getChineseName();
			String phone = "1234567";

			bean.setBuyerId(buyerId);
			bean.setName(name);
			bean.setPhone(phone);

			call.setData(bean);
			call.callService();

			Logger.verifyEquals("200", call.getString("code"), "验证Code");

			Logger.verifyIsNull(call.getSingleIdCardInfoResp().getIdCardInfo(),
					"校验接口返回IdCardInfo为null");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}